#include "StringUtil.h"
#include <windows.h>
#include <locale.h>
#include <stringapiset.h>
#include "PlatTypeUtil.h"
#include <vector>
#include <stdio.h>

std::string MGUtils::WS_To_S(const std::wstring& ws)
{
	std::string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
	setlocale(LC_ALL, "chs");
	const wchar_t* _Source = ws.c_str();
	uint64 _Dsize = 2 * ws.size() + 1;
	// char* _Dest = new char[_Dsize];
	static char _Dest[2048];
	memset(_Dest, 0, _Dsize);
	wcstombs(_Dest, _Source, _Dsize);
	std::string result = _Dest;
	delete[] _Dest;
	setlocale(LC_ALL, curLocale.c_str());
	return result;
}

std::wstring MGUtils::S_To_WS(const std::string& s)
{
	setlocale(LC_ALL, "chs");
	const char* _Source = s.c_str();
	uint64 _Dsize = s.size() + 1;
	// wchar_t* _Dest = new wchar_t[_Dsize];
	static wchar_t _Dest[2048];
	wmemset(_Dest, 0, _Dsize);
	mbstowcs(_Dest, _Source, _Dsize);
	std::wstring result = _Dest;
	delete[] _Dest;
	setlocale(LC_ALL, "C");
	return result;
}

std::string MGUtils::StringFormat(const char* fmt, ...)
{
	std::string  strResult = "";
	if (NULL != fmt)
	{
		va_list marker = NULL;
		va_start(marker, fmt);                             // 初始化变量参数 
		size_t nLength = _vscprintf(fmt, marker) + 1;     // 获取格式化字符串长度
		std::vector<char> vBuffer(nLength, '\0');         // 创建用于存储格式化字符串的字符数组
		int  nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
		if (nWritten > 0)
		{
			strResult = &vBuffer[0];
		}
		va_end(marker);                                     // 重置变量参数
	}
	return strResult;
}

std::wstring MGUtils::WStringFormat(const TCHAR* fmt, ...)
{
	std::wstring strResult = L"";
	if (NULL != fmt)
	{
		va_list marker = NULL;
		va_start(marker, fmt);                             // 初始化变量参数
		size_t nLength = _vscwprintf(fmt, marker) + 1;     // 获取格式化字符串长度
		std::vector<wchar_t> vBuffer(nLength, L'\0');     // 创建用于存储格式化字符串的字符数组
		int  nWritten = _vsnwprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
		if (nWritten > 0)
		{
			strResult = &vBuffer[0];
		}
		va_end(marker);                                     // 重置变量参数
	}
	return strResult;
}

MGUtils::StringConversion::StringConversion(const TCHAR* Str)
{
	Length = WideCharToMultiByte(CP_ACP, 0, Str, -1, NULL, 0, NULL, NULL);
	Ptr = new char[Length];
	WideCharToMultiByte(CP_ACP, 0, Str, -1, Ptr, Length, NULL, NULL);
}

MGUtils::StringConversion::StringConversion(const std::wstring& Str)
{
	Length = WideCharToMultiByte(CP_ACP, 0, Str.c_str(), -1, NULL, 0, NULL, NULL);
	Ptr = new char[Length];
	WideCharToMultiByte(CP_ACP, 0, Str.c_str(), -1, Ptr, Length, NULL, NULL);
}

MGUtils::StringConversion::StringConversion(StringConversion&& Other)
{
	Ptr = new char[Other.Length];
	Length = Other.Length;
	memcpy(Ptr, Other.Ptr, Length);
}

MGUtils::StringConversion::~StringConversion()
{
	delete Ptr;
	Ptr = nullptr;
}

const char* MGUtils::StringConversion::Get()
{
	return Ptr;
}

MGUtils::WStringConversion::WStringConversion(const char* Str)
{
	Length = MultiByteToWideChar(CP_ACP, 0, Str, static_cast<int>(strlen(Str) + 1), NULL, 0);
	Ptr = new TCHAR[Length];
	MultiByteToWideChar(CP_ACP, 0, Str, static_cast<int>(strlen(Str) + 1), Ptr, Length);
}

MGUtils::WStringConversion::WStringConversion(const std::string& Str)
{
	Length = MultiByteToWideChar(CP_ACP, 0, Str.c_str(), static_cast<int>(strlen(Str.c_str()) + 1), NULL, 0);
	Ptr = new TCHAR[Length];
	MultiByteToWideChar(CP_ACP, 0, Str.c_str(), static_cast<int>(strlen(Str.c_str()) + 1), Ptr, Length);
}

MGUtils::WStringConversion::WStringConversion(WStringConversion&& Other)
{
	Ptr = new TCHAR[Other.Length];
	Length = Length;
	memcpy(Ptr, Other.Ptr, Length);
}

MGUtils::WStringConversion::~WStringConversion()
{
	delete Ptr;
	Ptr = nullptr;
}

const TCHAR* MGUtils::WStringConversion::Get()
{
	return Ptr;
}